// home 页面 首页
app.controller('homeCtrl', [
    '$scope', '$state', 'ENV', 'httpService', '$timeout', 'loading', '$ionicSlideBoxDelegate', '$filter', '$localStorage', 'updateAppVersion',
    function($scope, $state, ENV, httpService, $timeout, loading, $ionicSlideBoxDelegate, $filter, $localStorage, updateAppVersion) {
        updateAppVersion.check();
        // 查询获取轮播图片  当没有轮播图的时候  则表明没有改用户 1000 秒退出
        var homeImageLen;
        $scope.getImages = function() {
            homeImageLen = 0;
            httpService.query('picscroll').then(function(res) {
                if (res.code === 200) {
                    $scope.images = [];
                    homeImageLen = res.data.length;
                    setTimeout(function() {
                        $ionicSlideBoxDelegate.update();
                        $scope.$apply(function() {
                            $scope.images = $localStorage.images = res.data;
                        });
                    });
                } else {
                    if (parseInt(res.data.code) === 444) {
                        loading.prompt(res.data.msg + '<br/>应用将于<strong>10</strong>秒后退出', 10000);
                        $timeout((function() {
                            ionic.Platform.exitApp();
                        }), 10000);
                    }
                }
            });
        };
        if ($localStorage.images) {
            $scope.images = $localStorage.images;
            $timeout(function() {
                $scope.getImages();
            }, 1000);
        } else {
            $scope.getImages();
        }

        function slideBox(index) {
            $ionicSlideBoxDelegate.slide(index, 500);
        }

        $scope.slideHasChanged = function(index) {
            if (homeImageLen == 2) {
                if (index == 2) {
                    slideBox(0);
                }
                if (index == 3) {
                    slideBox(1);
                }
            }
        };

        // targetType    '链接类型：1资讯，2医生(专家)，3设备',
        $scope.healthDetails = function(healthId, targetType) {
            if (targetType == 1) {
                $state.go('article_detail', {
                    id: healthId
                });
            } else if (targetType == 2) {
                $state.go('expert_detail', {
                    id: healthId
                });
            } else {
                $state.go('equipment_detail', {
                    id: healthId
                });
            }
        }

        // 过滤器  config中写好的参数 当为true的时候显示,  fore 不显示
        $scope.homes = $filter('falseFilter')(ENV.homes);
        $scope.isPhone = ENV.isPhone;
        $scope.arraies = [1, 2, 4];
        $scope.hospitalName = ENV.hospitalName;
        // 查询出文章信息
        $scope.getArticles = function(page) {
            httpService.query('v3/article').then(function(res) {
                if (res.code === 200) {
                    $scope.articles = res.data.body;
                }
            });
        };
        $scope.getArticles();
        $scope.homeRefresh = function() {
            $scope.getArticles();
            $timeout((function() {
                $scope.getImages();
            }), 50);
        };
    }
]);